<template>
  <div class="query">
    <van-nav-bar
      title="dolly绑定二级缓存"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="current_location_name"
        label="库位名称"
        size="large"
        name="current_location_name"
        clearable
        placeholder="请扫描当前库位名"
        :rules="[{ required: true, message: '请扫描当前库位名' }]"
      />

      <van-field
        v-model="object_name"
        clearable
        name="dolly号"
        label="dolly号"
        size="large"
        placeholder="请输入/扫描dolly二维码"
      />

      <van-field
        readonly
        clickable
        name="picker"
        size="large"
        :value="value"
        clearable
        label="dolly类型"
        placeholder="点击选择dolly类型"
        @click="showPicker = true"
      />
      <van-popup v-model="showPicker" position="bottom">
        <van-picker
          show-toolbar
          :columns="columns"
          @confirm="onConfirm"
          @cancel="showPicker = false"
        />
      </van-popup>
      <div style="margin: 16px">
        <van-button
          size="large"
          type="primary"
          style="margin-top: 20px"
          native-type="submit"
          >绑 定</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { SyncAdd, LocationInfo } from "@/api/api";
import { Toast } from "vant";
export default {
  name: "LibraryLocationQuery",
  data: () => {
    return {
      current_location_name: "",
      object_name: "",
      value: "空dolly",
      columns: ["空dolly", "满dolly"],
      columns2: ["左前门", "右前门"],
      showPicker: false,
    };
  },
  watch: {},
  created() {},
  async mounted() {},
  destroyed: function () {},
  methods: {
    onClickLeft() {
      this.$router.replace("/home");
    },
    onConfirm(value) {
      this.value = value;
      this.showPicker = false;
    },
    isSuoLocation() {
      return new Promise((resolve, reject) => {
        const params = { location_name: this.current_location_name };
        LocationInfo(params)
          .then((res) => {
            let is_booked = res.data[0].is_booked;
            resolve(is_booked);
          })
          .catch(() => {});
      });
    },
    onSubmit(values) {
      console.log("submit", values);
      // if (values.current_location_name) {
      //   this.isSuoLocation().then(res => {
      //     console.log("isSuoLocation===", res);

      //   })
      //   // this.bind();
      // }
      this.bind();
    },
    bind() {
      const params = {
        data: [
          {
            object_type_id: this.columns.indexOf(this.value) + 1,
            object_name: this.object_name,
            pallet_type: this.columns.indexOf(this.value) + 1,
            current_location_name: this.current_location_name,
            extra_info: {
              sku: "0",
              num: "0",
            },
            carrier_type_id: 1,
          },
        ],
        in_dispatch_type: 1,
      };
      console.log("SyncAdd==res", params);
      SyncAdd(params).then((res) => {
        console.log("SyncAdd==kes", res);
        if (res.code == 0) {
          if (res.data.error_list.length > 0) {
            Toast.fail(res.data.error_list[0].error);
          }
          if (res.data.success_list.length > 0) {
            Toast.success("操作成功");
          }
        }
      });
      // let [ERR, KRES] = await SyncAdd(params);
    },
  },
};
</script>

<style scoped>
.query {
  /* padding: 0 10px; */
  /* background: #eee; */
}
</style>
